/*
 * PratamaWijaya.com
 * set.mnemonix@gmail.com
 * States_MainMenu.java
 * AndEngineGamePanjatPinang
 * 7:15:07 PM 2012
 */

package com.amikomgamedev.panjatpinang.states;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;

import com.amikomgamedev.panjatpinang.CoolDownButton;
import com.amikomgamedev.panjatpinang.Data;
import com.amikomgamedev.panjatpinang.Define;
import com.amikomgamedev.panjatpinang.Game;
import com.amikomgamedev.panjatpinang.Utils;

import com.heyzap.sdk.HeyzapLib;

public class State_MainMenu extends SimpleBaseGameActivity implements Define,
		Data, IOnSceneTouchListener {
	private Camera	mCamera;
	private Scene	mScene;

	// resource spr bg
	private BitmapTextureAtlas	TexBg, TexImgBg, TexImgJudul;
	private ITextureRegion		TreImgBg, TreImgJudul;
	private TiledTextureRegion	TleBg;
	private AnimatedSprite		AspBg;
	private Sprite				sprImgBg, sprImgJudul;

	// heyzap icon
	private BitmapTextureAtlas	TexImgHeyzap;
	private ITextureRegion		TreImgHeyzap;
	private Sprite				SprImgHeyzap;

	private BitmapTextureAtlas	TexButton, texButtonHelp;
	private ITextureRegion		TreButtonPlay, TreButtonHighscore,
			TreButtonCredit, treButtonHelp;
	private Sprite				SprBtnPlay, SprBtnCredit, SprBtnScore,
			sprButtonHelp;

	// private Music mscBgmMenu;

	@Override
	public EngineOptions onCreateEngineOptions() {
		// TODO Auto-generated method stub
		Game.setContext(this);
		mCamera = new Camera(0, 0, GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT);
		EngineOptions en = new EngineOptions(true,
				ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(
						GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT), mCamera);
		en.getAudioOptions().setNeedsMusic(true);

		// load Heyzap
		HeyzapLib.load(this);
		return en;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		HeyzapLib.onActivityResult(requestCode, resultCode, data);
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onCreateResources() {
		// TODO Auto-generated method stub
		// background
		TexImgBg = new BitmapTextureAtlas(this.getTextureManager(),
				MENU_BACK_TEX_WIDTH, MENU_BACK_TEX_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TreImgBg = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				TexImgBg, this.getAssets(), IMG_MENU_UTAMA, 0, 0);

		TexBg = new BitmapTextureAtlas(this.getTextureManager(),
				MENU_BACK_TEX_WIDTH, MENU_BACK_TEX_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TleBg = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(
				TexBg, this.getAssets(), SPR_MENU_UTAMA, 0, 0,
				SPR_MAIN_MENU_COLUMN, SPR_MAIN_MENU_ROW);

		// button
		TexButton = new BitmapTextureAtlas(this.getTextureManager(),
				BUTTON_TEX_WIDTH, BUTTON_TEX_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		texButtonHelp = new BitmapTextureAtlas(this.getTextureManager(), 64,
				64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		treButtonHelp = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				texButtonHelp, this.getAssets(), IMG_MENU_BUTTON_HELP, 0, 0);

		TreButtonPlay = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				TexButton, this, IMG_BTN_PLAY, 0, 0);

		TreButtonHighscore = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexButton, this, IMG_BTN_LEADERBOARD, 107, 0);

		TreButtonCredit = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexButton, this, IMG_BTN_CREDIT, 212, 0);

		// judul
		TexImgJudul = new BitmapTextureAtlas(this.getTextureManager(), 256,
				128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TreImgJudul = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				TexImgJudul, this.getAssets(), IMG_LOGO_PANJAT_PINANG, 0, 0);

		// heyzap
		TexImgHeyzap = new BitmapTextureAtlas(this.getTextureManager(), 128,
				64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TreImgHeyzap = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				TexImgHeyzap, this.getAssets(), BTN_IMG_HEYZAP, 0, 0);

		Game.loadBgmMenu();

		TexBg.load();
		TexImgBg.load();
		TexButton.load();
		texButtonHelp.load();
		TexImgJudul.load();
		TexImgHeyzap.load();

	}

	@Override
	protected Scene onCreateScene() {
		// TODO Auto-generated method stub
		mEngine.registerUpdateHandler(new FPSLogger());

		Game.mscBgmMenu.play();

		mScene = new Scene();
		sprImgBg = new Sprite(0, 0, TreImgBg,
				mEngine.getVertexBufferObjectManager());

		AspBg = new AnimatedSprite(0, 0, TleBg,
				mEngine.getVertexBufferObjectManager());
		AspBg.animate(SPR_MAIN_MENU_SPEED[0], SPR_MAIN_MENU_FRAME[0], true);

		SprBtnPlay = new Sprite(BUTTON_PLAY_POS[0], BUTTON_PLAY_POS[1],
				TreButtonPlay, mEngine.getVertexBufferObjectManager());
		SprBtnCredit = new Sprite(BUTTON_CREDIT_POS[0], BUTTON_CREDIT_POS[1],
				TreButtonCredit, mEngine.getVertexBufferObjectManager());
		SprBtnScore = new Sprite(BUTTON_SCORE_POS[0], BUTTON_SCORE_POS[1],
				TreButtonHighscore, mEngine.getVertexBufferObjectManager());
		sprButtonHelp = new Sprite(BUTTON_HELP_POS[0], BUTTON_HELP_POS[1],
				treButtonHelp, mEngine.getVertexBufferObjectManager());

		sprImgJudul = new Sprite(0, 0, TreImgJudul,
				mEngine.getVertexBufferObjectManager());

		SprImgHeyzap = new Sprite(0, 0, TreImgHeyzap,
				mEngine.getVertexBufferObjectManager());
		SprImgHeyzap.setPosition(10, (GAME_RATIO_SCREEN_HEIGHT >> 1)
				- (SprImgHeyzap.getHeight() / 2));

		this.mScene.setOnSceneTouchListener(this);

		mScene.attachChild(sprImgBg);
		mScene.getLastChild().attachChild(AspBg);
		mScene.getLastChild().attachChild(sprImgJudul);
		mScene.getLastChild().attachChild(SprBtnPlay);
		mScene.getLastChild().attachChild(SprBtnCredit);
		mScene.getLastChild().attachChild(SprBtnScore);
		mScene.getLastChild().attachChild(sprButtonHelp);
		mScene.getLastChild().attachChild(SprImgHeyzap);

		return mScene;
	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, final TouchEvent eventClick) {
		// TODO Auto-generated method stub
		if (!CoolDownButton.getSharedInstance().checkValidity())
			return false;
		synchronized (this) {
			if (Utils.isInRect(eventClick.getX(), eventClick.getY(),
					SprBtnPlay.getX(), SprBtnPlay.getY(),
					SprBtnPlay.getWidth(), SprBtnPlay.getHeight())) {
				Game.mscBgmMenu.stop();
				Intent game = new Intent(State_MainMenu.this,
						State_GamePlay.class);
				startActivity(game);
				finish();
			} else if (Utils.isInRect(eventClick.getX(), eventClick.getY(),
					SprBtnCredit.getX(), SprBtnCredit.getY(),
					SprBtnCredit.getWidth(), SprBtnCredit.getHeight())) {
				Intent credit = new Intent(State_MainMenu.this,
						State_Credit.class);
				startActivity(credit);
				finish();
			} else if (Utils.isInRect(eventClick.getX(), eventClick.getY(),
					SprBtnScore.getX(), SprBtnScore.getY(),
					SprBtnScore.getWidth(), SprBtnScore.getHeight())) {
				Intent score = new Intent(State_MainMenu.this,
						State_Leaderboard.class);
				startActivity(score);
				finish();
			} else if (Utils.isInRect(eventClick.getX(), eventClick.getY(),
					sprButtonHelp.getX(), sprButtonHelp.getY(),
					sprButtonHelp.getWidth(), sprButtonHelp.getHeight())) {
				Intent help = new Intent(State_MainMenu.this, State_Help.class);
				startActivity(help);
				finish();
			} else if (Utils.isInRect(eventClick.getX(), eventClick.getY(),
					SprImgHeyzap.getX(), SprImgHeyzap.getY(),
					SprImgHeyzap.getWidth(), SprImgHeyzap.getHeight())) {
				// pencet heyzap
				onCheckinClicked();

			}
		}
		return true;
	}

	@Override
	public boolean onKeyDown(final int keyCode, final KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Utils.TRACE("exit pake back");
			finish();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}

	}

	public void onCheckinClicked() {
		HeyzapLib.checkin(this);
	}
}
